<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
use think\Config;
require_once '../extend/qiniu/autoload.php';
use Qiniu\Auth;
use Qiniu\Processing\PersistentFop;
use Qiniu\Processing\ImageUrlBuilder;

class Home extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
//        if($user_id != 0){
//            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//            if($auth == false){
//                echo json_encode(['status'=>301,'msg'=>"Token失效！请重新登录！"]);
//                exit;
//            }
//        }
    }
    /**
     * 获取七牛token
     * @return string
     */
    public function getQiniuToken(){//echo 1;die;
        require_once ('../extend/qiniu/autoload.php');
//        header("Access-Control-Allow-Origin:*");
        $accessKey = config('qiniu')['accessKey'];
        $secretKey = config('qiniu')['secretKey'];
        $bucket = 'shunhe';
//        $auth = new Qiniu();
        $auth = new Auth($accessKey, $secretKey);
        $token = $auth->uploadToken($bucket);
        return json_encode(array('msg'=>$token));
    }
    /*
     * 获取轮播图：getBanner
     */
    public function getBanner(){
        extract(input());
        $data = Db::name('lunbo')
            ->where(['lb_type'=>1,'lb_status'=>1])
//            ->field(['lb_id','lb_image'])
            ->order('lb_time DESC')
            ->limit (3)
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }

    /*
     * 获取轮播图内容：getBannerContent
     */
    public function getBannerContent(){
        extract(input());
        $data = Db::name('lunbo')
            ->where(['lb_id'=>$lunbo_id])
            ->field(['lb_id','lb_content'])
            ->find();
        return json(['status'=>200,'msg'=>$data]);
    }

    /**
     * 获取首页数据：精选房源、二手房推荐、新房源
     *  shi：当前定位的市id
     */
    public function getHomeData(){
        extract(input());
        $where['h_shi'] = $shi;
        $where['h_status'] = 1;
        $top1 = Db::table('up_top t')
            ->join ('up_house h','t.top_h_id = h.h_id','LEFT')
            ->where($where)
            ->where('top_have_one',1)
            ->where('h_id','>',0)
            ->field(['h_id','h_xiaoqu','h_shis','h_tings','h_mianji','h_img','h_street','h_type'])
            ->order('top_time ASC')
            ->limit (5)
            ->select ();
        //-----如果精选房源为空，则从置顶过的能拿出来的房源里随机选4个--start--
        if(!$top1){
            $hids_rand = Db::table('up_top t')
                ->join ('up_house h','t.top_h_id = h.h_id','LEFT')
                ->where($where)
                ->where('h_id','>',0)
                ->group('top_h_id')
                ->order('top_time DESC')
                ->limit(0,100)
                ->column('top_id');
            if($hids_rand){
                if(count($hids_rand) > 5){
                    $hids = [];
                    $keys = array_rand($hids_rand,5);
                    foreach ($keys as $v){
                        $hids[] = $hids_rand[$v];
                    }
                }else{
                    $hids = $hids_rand;
                }
                $top1 = Db::table('up_top t')
                    ->join ('up_house h','t.top_h_id = h.h_id','LEFT')
                    ->join ('up_area a','h.h_shi = a.district_id','LEFT')
                    ->where($where)
                    ->where('top_id','in',$hids)
                    ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','district','h_type','h_street'])
                    ->order('top_time ASC')
                    ->limit (5)
                    ->select ();
            }
        }
        //-----如果精选房源为空，则从置顶过的能拿出来的房源里随机选4个--end--
        //防止有重复的精选房源
        $chong_fu = [0];
        if($top1){
            foreach ($top1 as $k => $v){
                $chong_fu[] = $v['h_id'];
            }

        }
        $top2 = Db::table('up_house')
            ->where($where)
            ->where('h_have_top',1)
            ->where('h_id','not in',$chong_fu)
            ->field(['h_id','h_xiaoqu','h_hot_title','h_shis','h_tings','h_mianji','h_img','h_street','h_type'])
            ->order('h_time ASC')
            ->limit (5)
            ->select ();
        $top = array_merge($top1,$top2);
        $er = Db::table('up_top t')
            ->join ('up_house h','t.top_h_id = h.h_id','LEFT')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->where($where)
            ->where('top_type',2)
            ->where('top_have_two',1)
            ->where('h_id','>',0)
            ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','district','h_type'])
            ->order('top_time ASC')
            ->limit (5)
            ->select ();
        $new = Db::table('up_house h')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->where($where)
            ->where('h_type',3)
            ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','district','h_type'])
            ->order('h_time DESC')
            ->limit (5)
            ->select ();

        //-----如果二手房为空，则从置顶过的能拿出来的房源里随机选4个--start--
        if(!$er){
            $hids_rand = Db::table('up_top t')
                ->join ('up_house h','t.top_h_id = h.h_id','LEFT')
                ->where($where)
                ->where('top_type',2)
                ->where('h_id','>',0)
                ->group('top_h_id')
                ->order('top_time DESC')
                ->limit(0,100)
                ->column('top_id');
            if($hids_rand){
                if(count($hids_rand) > 4){
                    $hids = [];
                    $keys = array_rand($hids_rand,4);
                    foreach ($keys as $v){
                        $hids[] = $hids_rand[$v];
                    }
                }else{
                    $hids = $hids_rand;
                }
                $er = Db::table('up_top t')
                    ->join ('up_house h','t.top_h_id = h.h_id','LEFT')
                    ->join ('up_area a','h.h_shi = a.district_id','LEFT')
                    ->where($where)
                    ->where('top_id','in',$hids)
                    ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','district','h_type'])
                    ->order('top_time ASC')
                    ->limit (4)
                    ->select ();
            }
        }
        //-----如果二手房为空，则从置顶过的能拿出来的房源里随机选4个--end--
        //--------地图左边：按照第一个的类型展示---start-----------------------------------------------
        $left = array();
        if($top){
            $cankao = $top[0];
            $where['h_street'] = $cankao['h_street'];
            $where['h_xiaoqu'] = $cankao['h_xiaoqu'];
//            $where['h_type'] = $cankao['h_type'];
            $left = Db::table('up_house')
                ->where($where)
                ->where('h_type','>',1)
                ->field('h_xiaoqu,avg(h_price) price,avg(h_mianji) mianji,count(h_id) number,h_type')
                ->find();
            if($left['h_type'] > 1){
                $left['price'] = $left['price'] * 10000;
            }
            if($left['mianji'] <= 0){
                $left['price'] = 0;
            }else{
                $left['price'] = round($left['price']/$left['mianji'],2);
            }
        }
        //--------地图左边：按照第一个的类型展示----end-----------------------------------------------

        //--------1、推荐楼盘和推荐装饰公司---------------
        $builds = Db::table('up_building')
            ->where('b_shi',$shi)
            ->where('b_status',1)
            ->order('b_tuijiang desc,b_updatetime desc')
            ->field('b_id,b_title,b_title_bottom,b_img,b_mianji_min,b_mianji_max,b_unit_price,b_is_sale,b_huxing,b_hot_title,b_address')
            ->page(1,5)
            ->select();
        $coms = Db::table('up_company')
            ->where('shi',$shi)
//            ->where('tuijian',2)
            ->order('tuijian desc,time desc')
            ->field('id,address,thumb,name')
            ->page(1,5)
            ->select();
        //-------- end ------------------------------
        return json(['status'=>200,'top'=>$top,'er'=>$er,'new'=>$new,'left'=>$left,'builds'=>$builds,'coms'=>$coms]);
    }
    /**
     * 获得某市下的所有区及所属街道：getQuStreet
     * shi
     * @return string
     */
    public function getQuStreet()
    {
//        header("Content-type: text/html; charset=utf-8");
        extract(input());
        $pro = Db::table('up_area')
            ->where('pid',$shi)
            ->where('level',3)
            ->field('district_id,district')
            ->select();
        foreach($pro as $k=>$v){
            $city = Db::table('up_area')
                ->where('pid',$v['district_id'])
                ->where('level',4)
                ->field('district_id,district,pid')
                ->select();
            $pro[$k]['sub'] = $city;

        }
        return json_encode(['status'=>200,'msg'=>$pro]);
    }
    /**
     * 租房和新房二手房的列表及筛选：getHouseList
     * type：1是租房，2是二手房，3是新房
     * shi：当前定位的市id
     */
    public function getHouseList()
    {
        extract(input());
        $where = array();
        $where['h_type'] = $type;
        $where['h_status'] = 1;
        //1、区域筛选：shi、qu、street都要传
        if($qu){
            $where['h_qu'] = $qu;
        }else{
            $where['h_shi'] = $shi;
        }
        if($street){
            $where['h_street'] = $street;
        }
        //2、价格筛选:不限都为空，第一档的最小传1
//        if($price_min && $price_max){
        if($price_max){
            if(!$price_min){ $price_min = 0; }
            if($price_min == 8000 && $price_max == -1){
                $where['h_price'] = array('>=',$price_min);
            }elseif($price_min == 5000000 && $price_max == 10000){
                $where['h_price'] = array('>=',500);
            }else{
                if($type != 1){
                    $price_min = $price_min / 10000;
                    $price_max = $price_max / 10000;
                }
                $where['h_price'] = array('between',"$price_min,$price_max");
            }

        }
        //3、面积筛选：不限都为空，第一档的最小传1，最后一档的最大传1000000
        if($mianji_min && $mianji_max){
            $where['h_mianji'] = array('between',"$mianji_min,$mianji_max");
        }
        //4、更多里的
        if($huxing){//户型
            $where['h_shis'] = $huxing;
        }
        if($h_fix){//装修
            $where['h_fix'] = $h_fix;
        }
        if($h_face){//朝向
            $where['h_face'] = $h_face;
        }
        if($more){//更多信息:逗号隔开
            $more = trim($more,',');
            $moredata = explode(',',$more);
            if(count($moredata) > 1){
                $we1 = array();
                foreach($moredata  as $vo){
                    $we1[] = ['like',"%$vo%"];
                }
                $where['h_more'] = [$we1,'OR'];
            }else{
                $where['h_more'] = ['like','%'.$moredata[0].'%'];
            }
        }
        if($sign){//标签:逗号隔开
            $sign = trim($sign,',');
            $signdata = explode(',',$sign);
            if(count($signdata) > 1){
                $we = array();
                foreach($signdata  as $vo){
                    $we[] = ['like',"%,$vo,%"];
                }
                $where['h_sign'] = [$we,'OR'];
            }else{
                $where['h_sign'] = ['like','%,'.$signdata[0].',%'];
            }
        }
        if($user_type){//来源：1是用户  2是经纪人（别反了）
            $where['h_user_type'] = $user_type;
        }
        if($h_shui){//税费
            $where['h_shui'] = $h_shui;
        }
        if($h_kan){//看房
            $where['h_kan'] = $h_kan;
        }
        $data = Db::table('up_house h')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->where($where)
            ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','h_type','district'])
            ->order('h_order DESC,h_listtime DESC')
            ->page ($page,10)
            ->select ();
        return json(['status'=>200,'data'=>$data]);
    }
    //TODO:急售的那四个
    /**
     * 急售的那四个的数据列表
     * type：1是出租（出租），2是出售（二手房和新房）
     * shi：当前定位的市id
     */
    public function getFourSignData()
    {
        extract(input());
        $where = array();
        $where['h_shi'] = $shi;
        $where['h_status'] = 1;
        if(isset($sign) && $sign){
            $where['h_sign'] = array('like','%,'.$sign.',%');
        }
        $data = Db::table('up_house h')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->where($where)
            ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','h_type','district'])
            ->order('h_time DESC')
            ->page ($page,10)
            ->select ();
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 搜索:getSearchList
     * type：1是出租（出租），2是出售（二手房和新房）
     * shi：当前定位的市id
     */
    public function getSearchList()
    {
        extract(input());
        $where = array();
        $where['h_shi'] = $shi;
        $where['h_type'] = $type;
        $where['h_status'] = 1;
        if($type == 2){
            $where['h_type'] = array('in','2,3');
        }
        if($keyword){
            $data = Db::table('up_house h')
                ->join ('up_area a','h.h_shi = a.district_id','LEFT')
                ->where($where)
                ->where (function ($query) use ($keyword){
                    $query->where( 'h_xiaoqu',"like","%$keyword%")->whereOr( 'h_title',"like","%$keyword%");
                })
                ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','h_type','district'])
                ->order('h_order DESC,h_time DESC')
                ->page ($page,10)
                ->select ();
            return json(['status'=>200,'msg'=>$data]);
        }else{
            return json(['status'=>0,'msg'=>'关键字不能为空']);
        }
    }
    /**
     * 房源详情：houseInfo
     * h_id：房子id
     */
    public function houseInfo()
    {
        header('Access-Control-Allow-Origin:*');
        extract(input());
        $data = Db::table('up_house h')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->join ('up_u_user u','h.h_user_id = u.user_id','LEFT')
            ->where('h_id',$h_id)
            ->field('h.*,a.district,u.user_username,u.user_image,u.user_shop')
            ->find ();
        if(!$data['user_shop']){
            $data['user_shop'] = '';
        }
        $data['h_jianjie'] = nl2br($data['h_jianjie']);
        if(!$data){
            return json(['status'=>0,'msg'=>'该房源信息已被删除']);
        }
        $have = Db::table('up_like')
            ->where('like_user_id',$user_id)
            ->where('like_h_id',$h_id)
            ->find ();
        $data['h_images'] = explode(',',$data['h_images']);
        $data['h_sign'] = explode(',',trim($data['h_sign'],','));
        $data['h_more'] = explode(',',trim($data['h_more'],','));
        $like = 0;
        if($have){
            $like = 1;//已关注
        }
        //入历史记录----start-----
        if($user_id != 0){
            $insert = array();
            $insert['his_user_id'] = $user_id;
            $insert['his_h_id'] = $h_id;
            Db::table('up_history')->where($insert)->delete();
            $insert['his_date'] = date('Y-m-d H:i:s');
            Db::table('up_history')->insert($insert);
        }
        //入历史记录----end-------
        //---1、贷款
        $daikuan = [];
        if($data['h_type'] > 1){
            if($data['h_price'] >= 10000){
                $data['h_price'] = round($data['h_price']/10000,2);
            }
            $daikuan['allprice'] = $data['h_price'];
            $daikuan['shoufu'] = round(($data['h_price'] * $data['h_first'])/100,2);
            $daikuan['dai'] = $daikuan['allprice'] - $daikuan['shoufu'];
            $dknl = Db::table('up_about')->where('id',4)->value('content');
            $mww_dk =  new \app\api\controller\Show();
            $res = $mww_dk->debx(360,$daikuan['dai']*10000,$dknl);
            $daikuan['lixi'] = round($res['alllixi']/10000);
            $daikuan['yuegong'] = $res['data'][0]['month'];
        }
        //---2、所属小区信息
        $xiaoqu = [];
        if($data['h_dis_id'] > 0){
            $xiaoqu = Db::table('up_district')->where('dis_id',$data['h_dis_id'])->find();
        }
        //---3、装修风格推荐
        $cases = [];
        if($data['h_type'] > 1){
            $cases = Db::table('up_company_case')
                ->order('cc_time DESC')
                ->field('cc_id,cc_style,cc_thumb')
                ->page(1,3)
                ->select();
        }
        //---4、同小区经纪人推荐
        $tjuser = [];
        if($data['h_dis_id'] > 0){
            $uids = Db::table('up_house')
                ->where('h_type',$data['h_type'])
                ->where('h_status',1)
                ->where('h_dis_id',$data['h_dis_id'])
                ->where('h_user_id',"<>",$data['h_user_id'])
                ->column('h_user_id');

            if($uids){
                $tjuser =  Db::table('up_u_user')
                    ->where('user_type',2)
                    ->where('user_status',1)
                    ->where('user_id',"in",$uids)
                    ->order('user_reject_time DESC')
                    ->field('user_id,user_username,user_shop,user_image,user_phone')
                    ->page(1,3)
                    ->select();
            }

        }
        //---5、同小区房源推荐
        $tjhouse = [];
        if($data['h_dis_id'] > 0){
            $tjhouse = Db::table('up_house h')
                ->join ('up_area a','h.h_shi = a.district_id','LEFT')
                ->where('h_type',$data['h_type'])
                ->where('h_status',1)
                ->where('h_dis_id',$data['h_dis_id'])
                ->where('h_id',"<>",$data['h_id'])
                ->order('h_time DESC')
                ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','h_type','district'])
                ->page(1,3)
                ->select();
        }
        return json([
            'status'=>200,'msg'=>$data,'like'=>$like
            , 'daikuan' => $daikuan
            , 'xiaoqu' => $xiaoqu
            , 'cases' => $cases
            , 'tjuser' => $tjuser
            , 'tjhouse' => $tjhouse
        ]);
    }
    /**
     * 小区详情
     *
     */
    public function disInfo(){
        extract(input());
        $where = array();
        $where['dis_id'] = $dis_id;
        $data = Db::table('up_district')
            ->where($where)
            ->find ();
        if(!$data){
            return json(['status'=>100,'data'=>'此小区不存在或已下架！']);
        }
        //轮播图
        if($data['dis_images']){
            $data['dis_images'] = explode(',',$data['dis_images']);
        }
        //户型图和推荐
        $huxings = Db::table('up_district_hu')
            ->where('dh_type',1)
            ->where('dh_dis_id',$dis_id)
            ->select();
        //---4、同小区经纪人推荐
        $uids = Db::table('up_house')
            ->where('h_status',1)
            ->where('h_dis_id',$dis_id)
            ->column('h_user_id');
        $tjuser =  Db::table('up_u_user')
            ->where('user_type',2)
            ->where('user_status',1)
            ->where('user_id',"in",$uids)
            ->order('user_reject_time DESC')
            ->field('user_id,user_username,user_shop,user_image')
            ->page(1,4)
            ->select();
        //---5、同小区二手房源推荐
        $tjerhouse = Db::table('up_house h')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->where('h_type',2)
            ->where('h_status',1)
            ->where('h_dis_id',$dis_id)
            ->order('h_time DESC')
            ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','h_type','district'])
            ->page(1,3)
            ->select();
        //---5、同小区租户房源推荐
        $tjzuhouse = Db::table('up_house h')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->where('h_type',1)
            ->where('h_status',1)
            ->where('h_dis_id',$dis_id)
            ->order('h_time DESC')
            ->field(['h_id','h_title','h_hot_title','h_xiaoqu','h_price','h_shis','h_tings','h_mianji','h_img','h_type','district'])
            ->page(1,3)
            ->select();
        return json([
            'status'=>200,'msg'=>$data,'huxings'=>$huxings
            ,'tjuser'=>$tjuser
            ,'tjerhouse'=>$tjerhouse
            ,'tjzuhouse'=>$tjzuhouse
        ]);
    }
    /**
     * 关注或取消关注此房源：houseLike
     * h_id：房子id
     */
    public function houseLike()
    {
        extract(input());
        $have = Db::table('up_like')
            ->where('like_user_id',$user_id)
            ->where('like_h_id',$h_id)
            ->find ();
        if($have){
            $res = Db::table('up_like')
                ->where('like_user_id',$user_id)
                ->where('like_h_id',$h_id)
                ->delete ();
        }else{
            $data = array();
            $data['like_user_id'] = $user_id;
            $data['like_h_id'] = $h_id;
            $res = Db::table('up_like')->insert($data);
        }
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>0,'msg'=>'操作失败']);
        }

    }
    /**
     * 批量取消关注此房源：houseLike
     * h_id：房子id
     */
    public function houseCancelLikes()
    {
        extract(input());
        $h_id = trim($h_id,',');
        $res = Db::table('up_like')
            ->where('like_user_id',$user_id)
            ->where('like_h_id','in',$h_id)
            ->delete ();
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>0,'msg'=>'操作失败']);
        }

    }

    /**
     * 发布前的判断：待用（看前端）
     * name,avatar
     */
    public function newHouseBefore()
    {
        extract(input());
        $user = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->field('user_type,user_status,user_fabu,user_auth,user_all_fabu')
            ->find ();
        if($user['user_type'] > 1){
            if($user['user_status'] == 0){
                return json(['status'=>1,'msg'=>'认证审核中，请稍等']);
            }
            if($user['user_status'] == 4 || $user['user_status'] == 2){
                return json(['status'=>2,'msg'=>'请去认证']);
            }
            if($user['user_type'] == 3){
                return json(['status'=>200,'msg'=>'ok']);
            }
        }
        //
        $config = $this->getMemberConfig($user_id);
        $have_fabu = $config['mc_fabu'];
        if($user['user_fabu'] >= $have_fabu){
            return json(['status'=>0,'msg'=>'超过了发布次数']);
        }
        if($user['user_auth'] == 1 && $user['user_all_fabu'] >= $have_fabu){
            return json(['status'=>0,'msg'=>'您累计已经发布了' . $have_fabu . '次，请开通会员!']);
        }
        return json(['status'=>200,'msg'=>'ok']);
    }

    public function getDiss()
    {
        $data = Db::table('up_district')
            ->where('dis_shi',$_POST['shi'])
            ->order('dis_update DESC')
            ->field('dis_id,dis_name')
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }

    /**
     * 发布案例
     * @return \think\response\Json
     */
    public function newCase()
    {
        extract(input());
        $user = Db::table('up_u_user')->where('user_id',$user_id)->field('user_company_id_3,user_type,user_status')->find();
        if($user['user_type'] == 3 && $user['user_status'] == 1){
            $data = array();
            $data['cc_c_id'] = $user['user_company_id_3'];
            $data['cc_u_id'] = $user_id;
            $data['cc_title'] = $title;
            $data['cc_name'] = $name;
            $data['cc_style'] = $style;
            $data['cc_image'] = trim($image,',');
            $data['cc_thumb'] = explode(',',$data['cc_image'])[0];
            $data['cc_content'] = trim($content,',');
            $data['cc_time'] = date('Y-m-d H:i:s');
            $res = Db::table('up_company_case')->insert($data);
            if($res){
                Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_cases_3');
                return json(['status'=>200,'msg'=>'发布成功']);
            }else{
                return json(['status'=>0,'msg'=>'发布失败']);
            }
        }else{
            return json(['status'=>0,'msg'=>'只有认证成功的设计师才可以发布案例']);
        }

    }
    /**
     * 发布：
     * 取出缩略图
     * 发布次数判断
     * 经纪人权限判断
     * 排序3页以后处理
     *
    1、车位
    2、电梯
    3、车库
    4、花园
    5、储藏室
    6、阁楼
    7、床
    8、沙发
    9、电视
    10、冰箱
    11、热水器
    12、空调
     */
    public function newHouse()
    {
        extract(input());
        $config = $this->getMemberConfig($user_id);
        $user = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->field('user_type,user_status,user_auth,user_fabu,user_phone,user_all_fabu')
            ->find ();
        $have_fabu = $config['mc_fabu'];
        //
        if($user['user_auth'] == 1 && $user['user_all_fabu'] >= $have_fabu){
            return json(['status'=>0,'msg'=>'您累计已经发布了' . $have_fabu . '次，请开通会员!']);
        }
        //1、如果我是经纪人没有认证
        if($user['user_type'] == 2 && $user['user_status'] != 1){
            return json(['status'=>0,'msg'=>'请先去实名认证']);
        }
        //2、超过了发布次数
        if($user['user_fabu'] >= $have_fabu){
            return json(['status'=>0,'msg'=>'超过了发布次数']);
        }
        $data = array();
        $data['h_user_id'] = $user_id;
        $data['h_user_type'] = $user['user_type'];
        $data['h_title'] = $h_title;
        $data['h_images'] = trim($h_images,',');
        $data['h_img'] = explode(',',$data['h_images'])[0];
        $data['h_video'] = trim($h_video,',');
        $data['h_xiaoqu'] = $h_xiaoqu;
        $data['h_mianji'] = $h_mianji;
        $data['h_price'] = $h_price;
        if(isset($h_unit_price)){
            $data['h_unit_price'] = $h_unit_price*10000;
        }else{
            $data['h_unit_price'] = ($h_price/$h_mianji)*10000;
        }
        $data['h_shis'] = $h_shis;
        $data['h_tings'] = $h_tings;
        $data['h_weis'] = $h_weis;
        $data['h_fix'] = $h_fix;
        $data['h_floor'] = $h_floor;
        $data['h_first'] = $h_first;
        $data['h_sign'] = ',' . trim($h_sign,',') . ',';
//        $data['h_remark'] = $h_remark;
        $data['h_jianjie'] = $h_jianjie;
        $data['h_contact'] = $user['user_phone'];
        $data['h_type'] = $h_type;
        $data['h_sheng'] = $h_sheng;
        $data['h_shi'] = $h_shi;
        $data['h_qu'] = $h_qu;
        $data['h_street'] = $h_street;
        $data['h_lat'] = $h_lat;
        $data['h_lon'] = $h_lon;
        $data['h_time'] = date('Y-m-d H:i:s');
        $data['h_listtime'] = date('Y-m-d H:i:s');
        $data['h_updatetime'] = date('Y-m-d H:i:s');
        $data['h_order'] = $user['user_type'];
        $data['h_shui'] = $h_shui;
        $data['h_kan'] = $h_kan;
        $data['h_face'] = $h_face;
        $data['h_more'] = trim($h_more,',');
        $data['h_dis_id'] = $dis_id;
        $data['h_hot_title'] = trim($hot_title,' ');
		if($dis_id && $dis_id > 0){
                $llll = Db::table('up_district')->where('dis_id', $dis_id)->field('dis_lat,dis_lon')->find();
                $data['h_lat'] = $llll['dis_lat'];
                $data['h_lon'] = $llll['dis_lon'];
            }
        $res = Db::table('up_house')->insert($data);
        if($res){
            Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_fabu');
            Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_all_fabu');
            if ($user['user_type'] == 2) {
                $this->updateHouseList($h_type,$h_shi);
            }
            return json(['status'=>200,'msg'=>'发布成功']);
        }else{
            return json(['status'=>0,'msg'=>'发布失败']);
        }
    }

    public function qiniuCommonVideo()
    {
//        $auth22 = new ImageUrlBuilder();
//        $a = $auth22->waterText('http://qiniu.mangmeshi.com/1530783509305.jpg','5b6u6L2v6ZuF6buR');
//        dump($a);die;

        //对已经上传到七牛的视频发起异步转码操作
        $accessKey = 'nEztET3cvxjK87xXvVSOohxPd39zJTlO04jLgD74';
        $secretKey = 'ThogMprroyhdDO5q163ClfRQ_CBn8yXRsHAx1_Qk';
        $bucket = 'mangmeshi';
        $auth = new Auth($accessKey, $secretKey);
//要转码的文件所在的空间和文件名。http://qiniu.mangmeshi.com/1530504212846.mp4
        $key = '1530504212846.mp4';
//转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
        $pipeline = 'mang_mp4';
//转码完成后通知到你的业务服务器。
        $notifyUrl = 'http://mms.mangmeshi.com/admin/Api/index';
        $force = false;
        $config = new \Qiniu\Config();
//$config->useHTTPS=true;
        $pfop = new PersistentFop($auth, $config);
//需要添加水印的图片UrlSafeBase64
//可以参考http://developer.qiniu.com/code/v6/api/dora-api/av/video-watermark.html
        $base64URL = \Qiniu\base64_urlSafeEncode('顺和房产');
//水印参数
        $fops = "avthumb/mp4/s/640x360/vb/1.4m/wmText/$base64URL/wmFontColor/cmVk/wmFontSize/30/wmGravityText/NorthWest|saveas/"
            . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_wm8.mp4");
        list($id, $err) = $pfop->execute($bucket, $key, $fops, $pipeline, $notifyUrl, $force);
        echo "\n====> pfop avthumb result: \n";
        if ($err != null) {
            dump($err);
        } else {
            echo "PersistentFop Id: $id\n";
        }
//查询转码的进度和状态
        list($ret, $err) = $pfop->status($id);
        echo "\n====> pfop avthumb status: \n";
        if ($err != null) {
            dump($err);
        } else {
            dump($ret);
        }
    }

































public function getCityAndHot()
{
    $citys = Db::table('up_area')->where('level',2)->select();
    $data = array();
    foreach ($citys as $k=>$v){
        $str = $v['district'];
        $str= iconv("UTF-8","gb2312", $str);
        $tmp=bin2hex(substr($str,0,1));
        if($tmp>='B0'){ //汉字的开始
            $t=$this->getLetter(hexdec(bin2hex(substr($str,0,2))));
            $letter = chr($t);
            if($v['district'] == '衢州市'){$letter = 'Q';}
            if($v['district'] == '亳州市'){$letter = 'B';}
            if($v['district'] == '濮阳市'){$letter = 'P';}
            if($v['district'] == '漯河市'){$letter = 'L';}
            if($v['district'] == '儋州市'){$letter = 'D';}
            if($v['district'] == '泸州市'){$letter = 'L';}
            $data[$k]['id'] = $v['district_id'];
            $data[$k]['name'] = $v['district'];
            $data[$k]['letter'] = $letter;

        }
    }
    $last_names = array_column($data,'letter');
    array_multisort($last_names,SORT_ASC,$data);
    $start = '';
    $res = array();
    foreach ($data as $k=>$v){
        if($v['letter'] != $start){
            $start = $v['letter'];
        }
        $res[$start][] = $v;
    }
    //--------------------
    $hot = Db::table('up_area')
        ->where('level',2)
        ->where('hot',2)
        ->select();
    return json(['status'=>200,'msg'=>$res,'hot'=>$hot]);
//    dump($data);
//    dump($res);

}
    public function getLetter($num){
        $limit = array( //gb2312 拼音排序
            array(45217,45252), //A
            array(45253,45760), //B
            array(45761,46317), //C
            array(46318,46825), //D
            array(46826,47009), //E
            array(47010,47296), //F
            array(47297,47613), //G
            array(47614,48118), //H
            array(0,0),     //I
            array(48119,49061), //J
            array(49062,49323), //K
            array(49324,49895), //L
            array(49896,50370), //M
            array(50371,50613), //N
            array(50614,50621), //O
            array(50622,50905), //P
            array(50906,51386), //Q
            array(51387,51445), //R
            array(51446,52217), //S
            array(52218,52697), //T
            array(0,0),     //U
            array(0,0),     //V
            array(52698,52979), //W
            array(52980,53688), //X
            array(53689,54480), //Y
            array(54481,55289), //Z
        );
        $char_index=65;
        foreach($limit as $k=>$v){
            if($num>=$v[0] && $num<=$v[1]){
                $char_index+=$k;
                return $char_index;
            }
        }
        return -1;
    }

    /**
     * for tang ：根据用户IDS获取用户信息
     * @return \think\response\Json
     */
    public function getUsersInfo()
    {
        $uids = trim($_POST['uids'],',');
        $list = Db::table('up_u_user')
            ->field('user_id, user_username, user_image')
            ->where('user_id', 'in', $uids)
            ->select();
        return json(['status'=>200,'msg'=>$list]);
    }
}